#include <string.h>
#include "lsystem.h"

Lsystem_t readSystem() {
	Lsystem_t sysinfo;
	int i;
	char tmp;

	scanf("%c", &(sysinfo.move));
	scanf("%c", &tmp);
	scanf("%d", &(sysinfo.num_draw));
	scanf("%c", &tmp);

	sysinfo.draw = malloc(sizeof(char)*sysinfo.num_draw);

	//printf("%d\n", sysinfo.num_draw);
	for(i = 0; i < sysinfo.num_draw; i++) {
		//printf("  %d\n", i);
		scanf("%c", &(sysinfo.draw[i]));
	}

	scanf("%c", &tmp);

	scanf("%d", &(sysinfo.num_angles));
	scanf("%c", &tmp);
	sysinfo.angle_chars = malloc(sizeof(char)*sysinfo.num_angles);
	sysinfo.angles = malloc(sizeof(int)*sysinfo.num_angles);

	for(i = 0; i < sysinfo.num_angles; i++) {
		scanf("%c", &(sysinfo.angle_chars[i]));
		scanf("%c", &tmp);
		scanf("%d", &(sysinfo.angles[i]));
		scanf("%c", &tmp);
	}
	
	scanf("%d", &(sysinfo.num_rules));
	scanf("%c", &tmp);
	sysinfo.rule_predecessors = malloc(sizeof(char)*sysinfo.num_rules);
	sysinfo.rule_successors = malloc(sizeof(char*)*sysinfo.num_rules);

	for(i = 0; i < sysinfo.num_rules; i++) {
		scanf("%c", &(sysinfo.rule_predecessors[i]));
		scanf("%c", &tmp);
		sysinfo.rule_successors[i] = malloc(sizeof(char)*16);
		scanf("%s", sysinfo.rule_successors[i]);
		scanf("%c", &tmp);
	}

	sysinfo.start = malloc(sizeof(char)*8);
	scanf("%s", sysinfo.start);

	return sysinfo;
}

void printLsystem(Lsystem_t sysinfo) {
	int i;

	printf("move: %c\n", sysinfo.move);
	printf("# draw: %d\n", sysinfo.num_draw);
	for(i = 0; i < sysinfo.num_draw; i++) {
		printf("  %c\n", sysinfo.draw[i]);
	}

	printf("# angles: %d\n", sysinfo.num_angles);
	for(i = 0; i < sysinfo.num_angles; i++) {
		printf("%c: %d\n", sysinfo.angle_chars[i], sysinfo.angles[i]);
	}

	printf("# rules: %d\n", sysinfo.num_rules);
	for(i = 0; i < sysinfo.num_rules; i++) {
		printf("%c: %s\n", sysinfo.rule_predecessors[i], sysinfo.rule_successors[i]);
	}

	printf("start: %s\n", sysinfo.start);
}

char* getSuccessor(Lsystem_t sysinfo, char predecessor) {
	int i;

	for(i = 0; i < sysinfo.num_rules; i++) {
		if(sysinfo.rule_predecessors[i] == predecessor) {
			return sysinfo.rule_successors[i];
		}
	}

	return "";
}

int getAngle(Lsystem_t sysinfo, char angle_char) {
	int i;

	for(i = 0; i < sysinfo.num_angles; i++) {
		if(sysinfo.angle_chars[i] == angle_char)
			return sysinfo.angles[i];
	}

	return 0;
}

int isMove(Lsystem_t sysinfo, char move_char) {
	return sysinfo.move == move_char;
}

int isDraw(Lsystem_t sysinfo, char draw_char) {
	int i;

	for(i = 0; i < sysinfo.num_draw; i++) {
		if(draw_char == sysinfo.draw[i]) {
			return 1;
		}
	}

	return 0;

}
